package vip.xiaonuo.vein.impl.member;

import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.iservice.IMemberDeviceService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.mapper.MemberDeviceMapper;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.mapper.MemberUserMapper;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.vein.impl.adapter.VeinMemberReqAdapter;

import javax.annotation.Resource;

@Service(RuleConstant.VEIN + RuleConstant.MEMBER_DEVICESE)
public class VeinMemberDeviceServiceImpl implements IMemberDeviceService {

  @Resource
  private MemberUserMapper memberUserMapper;

  @Resource
  private MemberDeviceMapper memberDeviceMapper;

  @Resource
  private VeinMemberReqAdapter personService;

  @Resource
  private DeviceBaseService deviceBaseService;


  private void baseCheck(MemberDevice memberDevice) {
    if (memberDevice == null) {
      throw new ServiceException(500, "找不到关联信息！");
    }
    if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD)
            || memberDevice.getStatus().equals(MemberDeviceConstant.UPDATE)
            || memberDevice.getStatus().equals(MemberDeviceConstant.DELETE)) {
      throw new ServiceException(500, "非正常状态 不允许操作！");
    }
  }


  @Override
  public void delete(Long id) throws Exception {
    MemberDevice memberDevice = memberDeviceMapper.selectById(id);
    baseCheck(memberDevice);
    DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
    // 直接删除人员
    if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
      memberDeviceMapper.deleteById(id);
    } else {
      memberDevice.setStatus(MemberDeviceConstant.DELETE);
      memberDeviceMapper.updateById(memberDevice);
      personService.deletePerson(memberDevice, deviceBase.getId().toString());
    }
  }

  @Override
  public void sync(Long id) throws Exception {
    MemberDevice memberDevice = memberDeviceMapper.selectById(id);
    baseCheck(memberDevice);
    if (memberDevice.getStatus().equals(MemberDeviceConstant.SYNC_SUCCESS)) {
      throw new ServiceException(500, "未发生错误 不允许同步！");
    }
    DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
    MemberUser memberUser = memberUserMapper.selectById(memberDevice.getMemberId());
    // 恢复人员状态
    if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
      memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
      memberUser.setMemberType(1);
      personService.addPerson(memberDevice.getDeviceId() + "", memberUser);
    }

    if (memberDevice.getStatus().equals(MemberDeviceConstant.UPDATE_FAIL)) {
      memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
      memberUser.setMemberType(1);
      personService.updatePerson(memberDevice.getDeviceId() + "", memberUser);

    }
    if (memberDevice.getStatus().equals(MemberDeviceConstant.DELETE_FAIL)) {
      personService.deletePerson(memberDevice, deviceBase.getId().toString());
    }
    memberDeviceMapper.updateById(memberDevice);
  }
}
